<?php

if(!defined('INDEX'))
    die;

if($user->access < 3) {
    header('Location: /');
    exit;
}

require_once 'SmartyPaginate/libs/SmartyPaginate.class.php';
require_once NG.'classes/Admin.php';
require_once NG.'classes/Metalstorm.php';
require_once NG.'classes/Wikipedia.php';
require_once NG.'classes/Metacritic.php';


// ----------------------------------------------------------------------------

if(!empty($_REQUEST['op'])) {

    $op = trim($_REQUEST['op']);

    switch($op) {

        case 'visits':
            // required connect
            SmartyPaginate::connect();
            // set items per page
            SmartyPaginate::setLimit(100);

            $admin = new Admin;

            SmartyPaginate::setTotal($admin->getTotalVisits());
            SmartyPaginate::setUrl('/?cat=admin&op=visits');

            $smarty->assign('visits', $admin->getVisits(
                    SmartyPaginate::getLimit(),
                    SmartyPaginate::getCurrentIndex()
                ));

            SmartyPaginate::assign($smarty);

            break;






        case 'get_custom':

            $data = $_REQUEST['custom'];
            $date = date('Y-m-d', strtotime($_REQUEST['date']));

            if($date < date('Y-m-d'))
                break;

            $counter = 0;
            $artist = null;
            $release = null;

            $result = array();

            $data = preg_replace('/(\s\s)+/', "\n", $data);
            $data = preg_replace('/[\t]+/', "\n", $data);

            $data = explode("\n", $data);

            foreach($data as $line) {

                $line = trim($line);

                if(empty($line))
                    continue;

                //echo $line . "<BR>";
                //continue;
                    
                if($counter == 0) {
                    $artist = trim($line);
                    $counter++;
                } else if($counter == 1) {
                    $release = trim($line);
                    $counter = 0;
                    $result[] = array(
                        'date' => $date,
                        'artist' => $artist,
                        'release' => $release
                    );
                }
            }

            $smarty->assign('custom_releases', $result);

            break;

        case 'get_metacritic':

            $metacritic = new Metacritic;

            $smarty->assign('metacritic_releases', $metacritic->getReleases());

            break;

        case 'get_wikipedia':

            $wikipedia = new Wikipedia;

            $smarty->assign('wikipedia_releases', $wikipedia->getReleases());

            break;

        case 'get_metalstorm':

            $metalstorm = new Metalstorm;

            $smarty->assign('metalstorm_releases', $metalstorm->getReleases());

            break;

        case 'commit_wikipedia':
        case 'commit_metalstorm':
        case 'commit_custom':

            $releases = $_REQUEST['release'];

            $dbo = DBO::getInstance();

            $dbo->autoCommit(false);

            foreach($releases as $rel) {

                list($date, $artistname, $releasename) = explode('|', $rel, 3);

                if(empty($releasename))
                    continue;

                $release_year = date('Y');
                $release_date = $date;

                $release = new Release;

                $release->tba = (preg_match('/tba|untitled/i', $releasename));

                if($release->tba)
                    $release->name = 'To be announced';
                else
                    $release->name = $releasename;

                $release->release_date = $release_date;

                /* ARTIST */
                if(!Artist::exists($artistname)) {
                    $artist = new Artist;
                    $artist->name = $artistname;

                    $artist_id = $artist->save();

                    // Linking with lastfm tags
                    try {
                        $lastfm = new Lastfm;
                        $lastfm->updateArtistTags($artist_id);
                        $lastfm->updateArtistPicture($artist_id);
                    } catch (Exception $e) {
                        // TODO: log it
                    }

                } else {

                    $artist = Artist::getByName($artistname);

                }

                if(is_upcoming_date($release_date)) {

                    $chk_upcoming_id = $artist->hasUpcomingRelease($releasename);

                    if(!empty($chk_upcoming_id))
                        continue;

                    $release_id = $release->save();
                    $artist->linkWithRelease($release_id);

                } else {

                    if(!$artist->linkedWithRelease($release->name, $release_year)) {
                        $release_id = $release->save();
                        $artist->linkWithRelease($release_id);
                    }
                }
            }

            $dbo->commit();

            break;


    }
}